package com.hmis.hmis.UserInterface.checkController;

import com.hmis.dao.CheckItemDAO;
import com.hmis.dao.ItemDAO;
import com.hmis.dao.PatientDAO;
import com.hmis.domain.CheckItem;
import com.hmis.domain.Item;
import com.hmis.domain.Patient;
import com.hmis.domain.entityProperty.PatientProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.List;

public class Check {

    private static Check check;
    private PatientDAO patientDAO = PatientDAO.patientDAO;
    private ItemDAO itemDAO = ItemDAO.itemDAO;
    private CheckItemDAO checkItemDAO = CheckItemDAO.checkItemDAO;
    private HBox hBox = new HBox();

    private Check() {

    }

    public static Check getCheck() {
        if (check == null) {
            check = new Check();
        }
        return check;
    }

    public HBox gethBox() {
        initData();
        return hBox;
    }

    public void initData() {
        hBox.getChildren().clear();


        TableView table = new TableView();

        ObservableList<PatientProperty> data = FXCollections.observableArrayList();

        Button button = new Button("增加项目");

        ChoiceBox<String> choiceBox = new ChoiceBox();


        table.setEditable(true);

        // 从数据库获取病人数据
        List<Patient> patients = patientDAO.list();
        for (Patient patient : patients) {
            if (patient.getStatus() == 0) {
                data.add(new PatientProperty(patient.getPatientId(), patient.getPatientName(), patient.getWardNo(), patient.getTheBedNo()));
            }
        }

        TableColumn<PatientProperty, Long> patientIdCol = new TableColumn("病历号");
        patientIdCol.setMinWidth(100);
        patientIdCol.setCellValueFactory(
                new PropertyValueFactory<>("patientId"));

        TableColumn<PatientProperty, String> patientNameCol = new TableColumn("姓名");
        patientNameCol.setMinWidth(150);
        patientNameCol.setCellValueFactory(
                new PropertyValueFactory<>("patientName"));

        TableColumn<PatientProperty, Long> wardNoCol = new TableColumn("病房号");
        wardNoCol.setMinWidth(100);
        wardNoCol.setCellValueFactory(
                new PropertyValueFactory<>("wardNo"));

        TableColumn<PatientProperty, Long> bedNoCol = new TableColumn("床位号");
        bedNoCol.setMinWidth(100);
        bedNoCol.setCellValueFactory(
                new PropertyValueFactory<>("bedNo"));


        table.getColumns().addAll(patientIdCol, patientNameCol, wardNoCol, bedNoCol);
        table.setPrefHeight(500);
        table.setPrefWidth(450);
        table.setItems(data);


        choiceBox.getItems().add("验血");
        choiceBox.getItems().add("验大小便");
        choiceBox.getItems().add("做透视");
        choiceBox.getItems().add("做CT");
        choiceBox.getItems().add("B超");
        choiceBox.getItems().add("胃镜");

        TextArea textArea = new TextArea();
        textArea.setEditable(false);


        final VBox vbox = new VBox();
        vbox.getChildren().addAll(textArea, choiceBox, button);
        vbox.setVisible(false);
        vbox.setAlignment(Pos.CENTER);
        vbox.setSpacing(20);

        final Long[] patientId = new Long[1];
        table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<PatientProperty>() {
            @Override
            public void changed(ObservableValue<? extends PatientProperty> observable, PatientProperty oldValue, PatientProperty newValue) {
                patientId[0] = newValue.getPatientId();

                refreshText(patientId[0], textArea);


                vbox.setVisible(true);
            }
        });

        System.out.println(patientId[0]);

        hBox.setSpacing(20);
        hBox.setPadding(new Insets(10, 0, 0, 10));
        hBox.getChildren().addAll(table, vbox);
        hBox.setSpacing(80);

        // 添加项目
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String itemName = choiceBox.getValue();

                Stage msgBox = new Stage();
                Group msgGroup = new Group();
                Scene scene1 = new Scene(msgGroup, 230, 150);

                Label label = new Label("添加失败!");
                label.setLayoutX(80);
                label.setLayoutY(60);

                if (itemName != null) {

                    Item item = itemDAO.findByName(itemName);

                    if (!checkItemDAO.isValidCheckItem(patientId[0], item.getItemId(), 1)) {
                        Patient patient = patientDAO.findById(patientId[0]);

                        CheckItem checkItem = new CheckItem();
                        checkItem.setPatientId(patientId[0]);
                        checkItem.setPatientName(patient.getPatientName());
                        checkItem.setWardNo(patient.getWardNo());
                        checkItem.setBedNo(patient.getBedNo());
                        checkItem.setTheBedNo(patient.getTheBedNo());
                        checkItem.setItemId(item.getItemId());
                        checkItem.setStatus(1);
                        checkItem.setPrice(item.getPrice());

                        if (checkItemDAO.addCheckItem(checkItem)) {
                            label.setText("添加成功!");
                            refreshText(patientId[0], textArea);
                        }
                    } else {
                        label.setText("请勿重复添加!");
                    }
                } else {
                    label.setText("请选择项目!");
                }
                msgGroup.getChildren().add(label);

                msgBox.setScene(scene1);
                msgBox.show();

            }
        });
    }

    private void refreshText(long patientId, TextArea textArea) {
        List<CheckItem> checkItems = checkItemDAO.findCheckItemByPatientId(patientId);
        if (checkItems != null) {
            long sum = 0L;
            textArea.clear();
            textArea.appendText("已安排项目: ");
            for (CheckItem checkItem : checkItems) {
                Item item = itemDAO.findById(checkItem.getItemId());
                if (checkItem.getStatus() == 1) {
                    if (checkItem.getIsPay() == 0) {
                        sum += item.getPrice().longValue();
                        textArea.appendText("\n    " + item.getItemName() + "(未缴费: " + checkItem.getPrice() + "元).");
                    } else {
                        textArea.appendText("\n    " + item.getItemName() + "(已缴费).");
                    }
                }
            }
            textArea.appendText("\n共需缴费: " + sum + "元.");
        }
    }


}
